import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import usePatientsStore from '../../../store'

export default function (props) {
  const loading = ref(false)
  const dataSource = ref([])
  const columns = ref([])
  const patientsStore = usePatientsStore()
  watch(
    () => props.open,
    (val) => {
      if (val) {
        switch (props.type) {
          case '处置':
            getCz()
            break
          case '检验':
            getJy()
            break
          case '西成药':
            getXcy()
            break
          case '中草药':
            getZcy()
            break
          case '手术':
            getSs()
            break
        }
      } else {
        dataSource.value = []
        columns.value = []
      }
    }
  )
  //获取处置
  const getCz = async () => {
    loading.value = true
    columns.value = [
      { title: '处置名称', dataIndex: 'mc' },
      { title: '数量', width: 120, dataIndex: 'sl' },
      { title: '处置说明', dataIndex: 'yssm' }
    ]
    const { code, data, msg } = await post('/outp/mzys/getPatientTreatmentInfoList', {
      jzbh: patientsStore.currentPatient.jzbh,
      czsfck: 0
    })
    loading.value = false
    if (code !== 0) return message.error(msg || '请求失败')
    dataSource.value = data
  }
  //   //获取检查
  //   const getJc = async () => {
  //     loading.value = true
  //     columns.value = [
  //       { title: '处置名称', dataIndex: 'mc' },
  //       { title: '数量', width: 120, dataIndex: 'sl' },
  //       { title: '处置说明', dataIndex: 'yssm' }
  //     ]
  //     const { code, data, msg } = await post('/lis/LisApply/getLisApplyList', {
  //       pyjm: '',
  //       applyPath: 0,
  //       key: patientsStore.currentPatient.jzbh,
  //       isDisplayed: ''
  //     })
  //     loading.value = false
  //     if (code !== 0) return message.error(msg || '请求失败')
  //     dataSource.value = data
  //   }
  //获取检验
  const getJy = async () => {
    loading.value = true
    columns.value = [
      { title: '检验大类', width: 120, dataIndex: 'categroyName' },
      { title: '项目名称', dataIndex: 'items', ellipsis: true },
      { title: '报告时间', width: 160, dataIndex: 'bgsj' },
      { title: '申请时间', width: 160, dataIndex: 'applyTime' }
    ]
    const { code, data, msg } = await post('/lis/LisApply/getLisApplyList', {
      pyjm: '',
      applyPath: 0,
      key: patientsStore.currentPatient.jzbh,
      isDisplayed: ''
    })
    loading.value = false
    if (code !== 0) return message.error(msg || '请求失败')
    dataSource.value = data
  }
  //获取西成药
  const getXcy = async () => {
    loading.value = true
    columns.value = [
      { title: '药品名称', dataIndex: 'mc' },
      { title: '用法', width: 120, dataIndex: 'yf' },
      { title: '频率', dataIndex: 'pl' },
      { title: '剂量', dataIndex: 'jlsl' },
      { title: '天数', dataIndex: 'cfts' },
      { title: '总量', dataIndex: 'sl' },
      { title: '医生说明', dataIndex: 'yssm' }
    ]
    const { code, data, msg } = await post('/outp/mzys/getPatientPrescriptionInfoList', {
      jzbh: patientsStore.currentPatient.jzbh
    })
    loading.value = false
    if (code !== 0) return message.error(msg || '请求失败')
    dataSource.value = data
      .map((item) => item.children)
      .flat()
      .map((item) => {
        return {
          ...item,
          yf: item.yf.fyff,
          pl: item.pl.pl
        }
      })
  }
  //获取中草药
  const getZcy = async () => {
    loading.value = true
    columns.value = [
      { title: '处方名称', dataIndex: 'mc' },
      { title: '类型', width: 120, dataIndex: 'zylb' },
      { title: '付数', dataIndex: 'fssl' },
      { title: '单付数量', dataIndex: 'dfs' },
      { title: '总数量', dataIndex: 'zsl' },
      { title: '总金额', dataIndex: 'zje' },
      { title: '频率', dataIndex: 'pl' },
      { title: '用法', dataIndex: 'yf' },
      { title: '备注', dataIndex: 'bz' }
    ]
    const { code, data, msg } = await post('/outp/outpClinicalOrder/getOutpTCMHeadList', { jzbh: [patientsStore.currentPatient.jzbh] })
    loading.value = false
    if (code !== 0) return message.error(msg || '请求失败')
    dataSource.value = data.map((item) => item.children.map((c) => ({ ...c, ...item }))).flat()
  }
  //获取手术
  const getSs = async () => {
    loading.value = true
    columns.value = [
      { title: '手术名称', dataIndex: 'oprName' },
      { title: '手术部位', width: 120, dataIndex: 'ssbw' },
      { title: '手术时间', dataIndex: 'aptmOprTime' },
      { title: '手术房间', dataIndex: 'oprRoomName' },
      { title: '麻醉方式', dataIndex: 'anaesName' },
      { title: '联合手术一', dataIndex: 'jointOprName1' },
      { title: '联合手术二', dataIndex: 'jointOprName2' },
      { title: '手术医生', dataIndex: 'oprDoctorName' },
      { title: '麻醉医生', dataIndex: 'anaesDoctorName' },
      { title: '一助', dataIndex: 'firstNurseName' },
      { title: '二助', dataIndex: 'secondNurseName' }
    ]
    const { code, data, msg } = await post('/oprOperation/opr/getOperationAppointmentList', {
      keyLsh: patientsStore.currentPatient.jzbh,
      isInpAdv: '0'
    })
    loading.value = false
    if (code !== 0) return message.error(msg || '请求失败')
    dataSource.value = data
  }

  return { columns, loading, dataSource }
}
